﻿using Models;
using Repository.Interface;
using Service.Interface;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Service.Impl
{
    public class Services : IServices
    {
        private IBaseRepository _PackinforRepository;
        private SqlSugarClient _db;
        public Services(ISqlSugarClient db, IBaseRepository PackinforRepository)
        {
            _db = db as SqlSugarClient;
            _PackinforRepository = PackinforRepository;
        }

        public int Update()
        {
            string sql = "UPDATE PART_VALIDITY set STATUS = 1";
            return _db.GetConnection("LABEL").Ado.ExecuteCommand(sql.Replace(@"[\r\n]", ""));
        }

        public int Del()
        {
            string sql = "DELETE PART_VALIDITY ";
            return _db.GetConnection("LABEL").Ado.ExecuteCommand(sql.Replace(@"[\r\n]", ""));
        }

        public int Inert()
        {
            string sql = "INSERT INTO PART_VALIDITY (PART_FLAG, PART_NO, STATUS, START_TIME, END_TIME, CREATION, CREATION_TIME, MODIFIER, MODIFIER_TIME) VALUES ('0', '7118-012-01', '0', TO_DATE('2023-06-30 09:59:00', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2023-06-30 09:59:00', 'SYYYY-MM-DD HH24:MI:SS'), 'ADMIN', TO_DATE('2023-06-30 09:59:33', 'SYYYY-MM-DD HH24:MI:SS'), 'ADMIN', TO_DATE('2023-06-30 00:00:00', 'SYYYY-MM-DD HH24:MI:SS'))";
            return _db.GetConnection("LABEL").Ado.ExecuteCommandWithGo(sql.Replace(@"[\r\n]", ""));
        }

        public List<TEntity> ListTentity<TEntity>()
        {

            /*
            //根据特性直接CRUD
            var list=db.QueryableWithAttrWithAttr<Order>().ToList();//5.0.9.1 全自动切换库查询
            db.InsertWithAttr(list).ExecuteCommand() ;//5.0.9.1 全自动切换库插入
            db.UpdateWithAttr(list).ExecuteCommand() ;//5.0.9.1 全自动切换库更新
            db.DeleteableWithAttr(list).ExecuteCommand() ;//5.0.9.1 全自动切换库删除
            */
            //Expression<Func<Ppackinfor, bool>> filter = item => item.ID.Equals(2);
            //var ui = _db.DeleteableWithAttr<Ppackinfor>().Where(filter).ExecuteCommand();
            //var inset = _db.InsertableWithAttr(list).ExecuteCommand();
            //Task<bool> task = _repository.InsertData(model);

            //获取数据库链接对象 
            /*
            方法一：
            var dba = _db.GetConnectionScopeWithAttr<TEntity>();
            var list = dba.Queryable<TEntity>().ToList();
            方法二：
            var dbcontext = _db.QueryableWithAttr<TEntity>().Context;
            var list = dbcontext.Queryable<TEntity>().ToList();
            方法三：不获取dbConnection  自动跨库
            var list = _db.QueryableWithAttr<TEntity>().ToList();
             */

            var list = _db.QueryableWithAttr<TEntity>().ToList();
            //var lists = _PackinforRepository.SelectList<TEntity>();

            ////_db.BeginTran();
            //Ppackinfor ppackinfor = new Ppackinfor();
            //ppackinfor.BOX = "box";
            //ppackinfor.LOT = "LOT";
            //ppackinfor.PACK = "YYYY";
            //ppackinfor.PCS = 1111111111;
            //ppackinfor.TUOPAN = "OOOO";

            //var inset = _db.InsertableWithAttr(ppackinfor).ExecuteCommand();

            //_db.RollbackTran();

            return list;
        }

        public List<Ppackinfor> List()
        {
            throw new NotImplementedException();
        }
    }
}
